package com.workcell.workdata.xthings.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.workcell.workdata.xthings.entity.UserInfo;
import com.workcell.workdata.xthings.mapper.UserInfoMapper;
import com.workcell.workdata.xthings.service.UserInfoService;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 打卡机员工表 服务实现类
 * </p>
 *
 * @author zhangguangrui
 * @since 2023-03-15
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {

    @Override
    public Boolean addtimeClockUser(com.workcell.workdata.xthings.dto.UserInfo userInfo) {

        //打卡机下发人员
        this.addUserByISAPI(userInfo);
        //人员信息添加数据库
        UserInfo info = BeanUtil.copyProperties(userInfo, UserInfo.class);
        return this.save(info);

    }

    //下发人员
    public String addUserByISAPI(com.workcell.workdata.xthings.dto.UserInfo userInfo) {

//
        String json = "{\"UserInfo\":{\"employeeNo\":\""+ userInfo.getEmployeeNo() +"\",\"name\":\""+userInfo.getName()+ "\",\"userType\":\""+ userInfo.getUserType()+ "\",\"Valid\":{\"enable\":"+ userInfo.getValid().getEnable() + ",\"beginTime\":\""  +userInfo.getValid().getBeginTime() + "\",\"endTime\":\"" + userInfo.getValid().getBeginTime() + "\",\"timeType\":\""+ userInfo.getValid().getTimeType() +"\"}}}";

        HttpClient defaultHttpClient = httpClient();
        HttpClient digestHttpClient = digestHttpClient("admin","cw123456",null,null);

        HttpPut httpPut = new HttpPut("10.10.30.91/ISAPI/AccessControl/UserInfo/SetUp?format=json");
        httpPut.setHeader("Content-Type", "application/json");

        try {
            StringEntity stringEntity = new StringEntity(json);
            stringEntity.setContentType("application/json");
            httpPut.setEntity(stringEntity);
            System.out.println("=============digest auth=====================");
            System.out.println(EntityUtils.toString(digestHttpClient.execute(httpPut).getEntity()));
            System.out.println();
        } catch (Exception e) {
            e.printStackTrace();

        }
        return null;

    }


    public static HttpClient digestHttpClient(String username, String password, String host, Integer port){
        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(new AuthScope(StringUtils.isBlank(host) ? AuthScope.ANY_HOST : host, port == null ? AuthScope.ANY_PORT : port),
                new UsernamePasswordCredentials(username,password));
        return HttpClients.custom().setDefaultCredentialsProvider(credentialsProvider).build();
    }

    public static HttpClient httpClient(){
        return HttpClients.custom().build();
    }





}
